﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ARR.Entities;
using System.Collections.ObjectModel;
using ARR.UI.ArticlesService;
using DDF.Entity;

namespace ARR.UI.Models
{
    public interface IPIDifferencesImporterModel : IModelBase
    {
        List<ImporterResult> Results { get; set; }

        ObservableCollection<TempPIDifference> TempPIDifferences { get; set; }

        ObservableCollection<TempPIDifference> TempPIDifferencesCache { get; set; }

        void Save();
    }

    public class PIDifferencesImporterModel : ModelBase, IPIDifferencesImporterModel
    {
        public PIDifferencesImporterModel()
        {
            this.Results = new List<ImporterResult>();
            this.TempPIDifferences = new ObservableCollection<TempPIDifference>();
            this.TempPIDifferencesCache = new ObservableCollection<TempPIDifference>();
        }

        public List<ImporterResult> Results { get; set; }

        public ObservableCollection<TempPIDifference> TempPIDifferences { get; set; }

        public ObservableCollection<TempPIDifference> TempPIDifferencesCache { get; set; }

        public void Save()
        {
            var toSave = TempPIDifferencesCache.Where(x => x.GetEntityState() != EntityState.Original);

            if (toSave.Any())
            {
                ArticlesServiceClient service = new ArticlesServiceClient();

                try
                {
                    service.SaveTempPIDifferences(toSave.ToList());
                    service.Close();
                }
                catch
                {
                    service.Abort();
                    throw;
                }
            }
        }
        
    }


    public class PIDifferencesImporterModelMock : MockModelBase, IPIDifferencesImporterModel
    {
        public PIDifferencesImporterModelMock()
        {
            this.Results = new List<ImporterResult>();
            this.TempPIDifferences = new ObservableCollection<TempPIDifference>();
            this.TempPIDifferencesCache = new ObservableCollection<TempPIDifference>();
        }

        public List<ImporterResult> Results { get; set; }

        public ObservableCollection<TempPIDifference> TempPIDifferences { get; set; }

        public ObservableCollection<TempPIDifference> TempPIDifferencesCache { get; set; }

        public void Save()
        {

        }

    }
}
